{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find the Number of Islands\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/fF4UY2gX5qlbkpRnJjtW5g)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a 2-dimensional grid consisting of `1`'s (land blocks) and `0`'s (water blocks), count the number of islands present in the grid. The definition of an island is as follows:\n",
    "\n",
    "1. Must be surrounded by water blocks.\n",
    "2. Consists of land blocks (`1`'s) connected to adjacent land blocks (either vertically or horizontally). \n",
    "\n",
    "Assume all edges outside of the grid are water."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: \n",
    "10001\n",
    "11000\n",
    "10110\n",
    "00000\n",
    "\n",
    "Output: 3\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    ''' Class Definition '''\n",
    "    \n",
    "    def inRange(self, grid, r, c):\n",
    "        ''' Range Check '''\n",
    "        (numRow, numColumn) = (len(grid), len(grid[0]))\n",
    "        if r < 0 or c < 0 or r >= numRow or c >= numColumn:\n",
    "            return False\n",
    "        return True\n",
    "    \n",
    "    def sinkIslands(self, grid, row, column):\n",
    "        ''' Sink an Island '''\n",
    "        if grid[row][column] == 1:\n",
    "            grid[row][column] = 0\n",
    "        else:\n",
    "            return\n",
    "        \n",
    "        # Sink the land besides recursively.\n",
    "        if self.inRange(grid, row + 1, column):\n",
    "            self.sinkIslands(grid, row + 1, column)\n",
    "        if self.inRange(grid, row - 1, column):\n",
    "            self.sinkIslands(grid, row - 1, column)\n",
    "        if self.inRange(grid, row, column + 1):\n",
    "            self.sinkIslands(grid, row, column + 1)\n",
    "        if self.inRange(grid, row, column - 1):\n",
    "            self.sinkIslands(grid, row, column - 1)\n",
    "    \n",
    "    def numIslands(self, grid):\n",
    "        ''' Count Islands '''\n",
    "        count = 0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j] == 1:\n",
    "                    count += 1\n",
    "                    self.sinkIslands(grid, i, j)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "''' Test Scipts '''\n",
    "grid = [\n",
    "    [1, 1, 0, 0, 0],\n",
    "    [0, 1, 0, 0, 1],\n",
    "    [1, 0, 0, 1, 1],\n",
    "    [0, 0, 0, 0, 0]\n",
    "]\n",
    "print(Solution().numIslands(grid))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
